package com.mbbb.yygh.hosp.testmongo;

import com.mbbb.yygh.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "mongodb接口2")
@RestController
@RequestMapping("/mongo2")
public class TestMongo2 {

    @Autowired
    private UserRepository userRepository;

    //添加
    @ApiOperation("添加")
    @PostMapping("/create")
    public R createUser() {
        User user = new User();
        user.setName("熊二");
        user.setAge(38);
        user.setEmail("352634@qq.com");

        User user1 = userRepository.save(user);
        return R.ok().data("user1",user1);
    }

    //查询所有
    @ApiOperation("查询所有")
    @GetMapping("/findAll")
    public R findUser() {
        List<User> list = userRepository.findAll();
        return R.ok().data("list",list);
    }

    //id查询
    @ApiOperation("id查询")
    @GetMapping("/findId/{id}")
    public R getById(@PathVariable String id) {
        User user = userRepository.findById(id).get();
        return R.ok().data("user",user);
    }

    //条件查询
    @ApiOperation("条件查询")
    @GetMapping("/findQuery")
    public R findUserList() {
        User user = new User();
        user.setName("熊二");
        user.setAge(38);

        Example<User> example = Example.of(user);
        List<User> list = userRepository.findAll(example);
        return R.ok().data("list",list);
    }

    //模糊查询
    @ApiOperation("模糊查询")
    @GetMapping("/findLike")
    public R findUsersLikeName() {
        //创建匹配器，即如何使用查询条件
        ExampleMatcher exampleMatcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        User user = new User();
        user.setName("二");
        Example<User> example = Example.of(user, exampleMatcher);
        List<User> list = userRepository.findAll(example);
        return R.ok().data("list",list);
    }

    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("/findPage")
    public R findUsersPage() {
        //设置排序
        Sort sort = Sort.by(Sort.DEFAULT_DIRECTION, "age");
        //0为第一页（当前页）
        Pageable pageable = PageRequest.of(0, 1, sort);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()//构建对象
                           .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                           .withIgnoreCase(true);//改变默认大小写忽略方式：忽略大小写

        User user = new User();
        user.setName("二");

        Example<User> example = Example.of(user, matcher);

        Page<User> list = userRepository.findAll(example, pageable);
        List<User> content = list.getContent();
        long totalElements = list.getTotalElements();
        return R.ok().data("list",list).data("content",content).data("totalElements",totalElements);
    }

    //根据id修改
    @ApiOperation("根据id修改")
    @GetMapping("/update/{id}")
    public R updateUser(@PathVariable String id) {
        User user = userRepository.findById(id).get();
        user.setAge(22);
        User save = userRepository.save(user);
        return R.ok().data("save",save);
    }

    //根据id删除
    @ApiOperation("根据id删除")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        userRepository.deleteById(id);
        return R.ok();
    }

    //where name=? and age=?
    @GetMapping("/nameAndAge")
    public R nameAndAge() {
        List<User> list = userRepository.findUserByNameAndAge("阿光",20);
        return R.ok().data("list",list);
    }

    //where name like ?
    @GetMapping("/nameLike")
    public R nameLike() {
        List<User> list = userRepository.findUserByNameLike("二");
        return R.ok().data("list",list);
    }
}
